﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class BatchStationRepository
    {
        // 添加批量站
        public int AddBatchStation(BatchStation batchStation)
        {
            string sql = @"INSERT INTO batchstation 
                            (ProductId, ProcessPackageId, UserId, Remark) 
                           VALUES 
                            (@ProductId, @ProcessPackageId, @UserId, @Remark)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@ProductId", batchStation.ProductId),
                new MySqlParameter("@ProcessPackageId", batchStation.ProcessPackageId),
                new MySqlParameter("@UserId", batchStation.UserId),
                new MySqlParameter("@Remark", batchStation.Remark ?? (object)DBNull.Value)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除批量站
        public int DeleteBatchStation(int batchStationId)
        {
            string sql = "DELETE FROM batchstation WHERE BatchStationId = @BatchStationId";
            MySqlParameter[] parameters = { new MySqlParameter("@BatchStationId", batchStationId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新批量站信息
        public int UpdateBatchStation(BatchStation batchStation)
        {
            string sql = @"UPDATE batchstation 
                            SET ProductId = @ProductId, 
                                ProcessPackageId = @ProcessPackageId, 
                                UserId = @UserId, 
                                Remark = @Remark 
                          WHERE BatchStationId = @BatchStationId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@ProductId", batchStation.ProductId),
                new MySqlParameter("@ProcessPackageId", batchStation.ProcessPackageId),
                new MySqlParameter("@UserId", batchStation.UserId),
                new MySqlParameter("@Remark", batchStation.Remark ?? (object)DBNull.Value),
                new MySqlParameter("@BatchStationId", batchStation.BatchStationId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 查询所有批量站
        public List<BatchStation> GetAllBatchStations()
        {
            string sql = "SELECT * FROM batchstation";
            return DBHelper.ExecuteQuery(sql, null, MapBatchStation);
        }

        // 查询单个批量站
        public BatchStation GetBatchStationById(int batchStationId)
        {
            string sql = "SELECT * FROM batchstation WHERE BatchStationId = @BatchStationId";
            MySqlParameter[] parameters = { new MySqlParameter("@BatchStationId", batchStationId) };
            var batchStations = DBHelper.ExecuteQuery(sql, parameters, MapBatchStation);
            return batchStations.FirstOrDefault();
        }

        // 映射数据库查询结果到 BatchStation 对象
        private BatchStation MapBatchStation(MySqlDataReader reader)
        {
            return new BatchStation
            {
                BatchStationId = (int)reader["BatchStationId"],
                ProductId = (int)reader["ProductId"],
                ProcessPackageId = (int)reader["ProcessPackageId"],
                UserId = (int)reader["UserId"],
                Remark = reader["Remark"] is DBNull ? null : reader["Remark"].ToString()
            };
        }
    }
}